<?php

namespace app\api\module;

use app\common\model\CommunityRanking;
use app\common\model\HouseRanking;
use app\common\model\MapSearch;
use app\common\model\Rental as RentalModel;
use app\common\model\RentalCategory;
use app\common\model\RentalSearch;
use think\facade\Db;

class Ranking extends Common
{
    /**
     * 判断是否需要登录
     *
     * @param string $interface 待检测的接口
     * @return bool
     */
    protected function needLogin($interface)
    {
        //不需登录的接口
        $ignoreList = [
            ZFN_RANKING_SEARCH_DATA,
            ZFN_RANKING_HOUSE,
            ZFN_RANKING_COMMUNITY,

            ZFN_MAP_SECOND_SEARCH_DATA,
            ZFN_MAP_RENTAL_SEARCH_DATA,
            ZFN_MAP_SECOND_LIST,
            ZFN_MAP_RENTAL_LIST,
            ZFN_INDEX_RANKING_LIST,
        ];

        if (in_array($interface, $ignoreList)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 执行接口操作
     *
     * @param $interface
     * @return array|string
     */
    public function exec($interface)
    {
        switch ($interface) {
            //【1001】头部及筛选项
            case ZFN_RANKING_SEARCH_DATA:
                return $this->searchData();
            //【1002】房源排行榜
            case ZFN_RANKING_HOUSE:
                return $this->houseRanking();
            //【1003】热搜小区榜
            case ZFN_RANKING_COMMUNITY:
                return $this->communityRanking();

            //【1004】地图找二手房筛选项目
            case ZFN_MAP_SECOND_SEARCH_DATA:
                return $this->mapSecondSearchData();
            //【1005】地图找租房筛选项目
            case ZFN_MAP_RENTAL_SEARCH_DATA:
                return $this->mapRentalSearchData();
            //【1006】地图二手房列表
            case ZFN_MAP_SECOND_LIST:
                return $this->mapSecondList();
            //【1007】地图租房列表
            case ZFN_MAP_RENTAL_LIST:
                return $this->mapRentalList();
            //【1008】首页排行榜
            case ZFN_INDEX_RANKING_LIST:
                return $this->indexRankingList();
            default:
                send_developer_error("模块【排行榜】中不存在接口【" . $interface . "】,核实文档后重试!");
        }
    }
    //首页排行列表
    private function indexRankingList()
    {
        $user = $this->currentUser(false);
        $city_id = $this->optionalParam('city_id');
        if(empty($city_id)){
            $city_id=1205;
        }
        //热门新上
        $one = HouseRanking::getRanking($user,1,$city_id,0,0,0,0,0,1,3);
        //捡漏
        $two = HouseRanking::getRanking($user,2,$city_id,0,0,0,0,0,1,3);
        //热搜房源
        $three = HouseRanking::getRanking($user,3,$city_id,0,0,0,0,0,1,3);
        //人气小区
        $four = CommunityRanking::getRanking($user,4,$city_id,0,0,1,3);
        //热搜小区
        $five = CommunityRanking::getRanking($user,5,$city_id,0,0,1,3);
        $data = [['type' => 1,'title' => '热门新上榜','date_desc' => date("Y-m-d").'更新','data' => $one],
                ['type' => 2,'title' => '捡漏榜','date_desc' => date("Y-m-d").'更新','data' => $two],
                ['type' => 3,'title' => '热搜房源榜','date_desc' => date("Y-m-d").'更新','data' => $three],
                ['type' => 4,'title' => '人气小区榜','date_desc' => date("Y-m-d").'更新','data' => $four],
                ['type' => 5,'title' => '热搜小区榜','date_desc' => date("Y-m-d").'更新','data' => $five]];
        $this->insertResponse('data',$data);
        return $this->response('首页排行列表');
    }

    private function searchData()
    {
        $city_id = $this->essentialParam('city_id');
        $data = HouseRanking::searchData($city_id);
        $this->insertResponse('',$data);
        return $this->response('头部及筛选项');
    }

    private function houseRanking()
    {
        $user = $this->currentUser(false);
        $type = $this->essentialParam('type');
        $city_id = $this->essentialParam('city_id');
        $area_id = $this->optionalParam('area_id');
        $district_id = $this->optionalParam('district_id');
        $price_min = $this->optionalParam('price_min');
        $price_max = $this->optionalParam('price_max');
        $bedroom = $this->optionalParam('bedroom');

        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $data = HouseRanking::getRanking($user,$type,$city_id,$area_id,$district_id,$price_min,$price_max,$bedroom,$page,$page_size);
        $this->insertResponse('data',$data);
        return $this->response('房源排行榜列表');
    }

    private function communityRanking()
    {
        $user = $this->currentUser(false);
        $type = $this->essentialParam('type');
        $city_id = $this->essentialParam('city_id');
        $area_id = $this->optionalParam('area_id');
        $district_id = $this->optionalParam('district_id');

        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $data = CommunityRanking::getRanking($user,$type,$city_id,$area_id,$district_id,$page,$page_size);
        $this->insertResponse('data',$data);
        return $this->response('小区排行榜列表');
    }


    private function mapSecondSearchData(){
        $city_id = $this->essentialParam('city_id');
        $longitude = $this->optionalParam('longitude');
        $latitude = $this->optionalParam('latitude');
        $source = $this->optionalParam('source');
        $data = MapSearch::secondSearchData($city_id,$longitude,$latitude,$source);
        $this->insertResponse('',$data);
        return $this->response('地图找房二手房筛选项目');
    }

    private function mapRentalSearchData(){
        $city_id = $this->essentialParam('city_id');
        $longitude = $this->optionalParam('longitude');
        $latitude = $this->optionalParam('latitude');
        $source = $this->optionalParam('source');
        $data = MapSearch::rentalSearchData($city_id,$longitude,$latitude,$source);
        $this->insertResponse('',$data);
        return $this->response('地图找房租房筛选项目');
    }

    private function mapSecondList()
    {
        $city_id = $this->essentialParam('city_id');
        //模仿贝壳 贝壳的区域字段变动并没有重新选择数据 若客户要求可以使用下面注释接收变量
    //    $area_id = 0;
        $district_ids = '';
        $nearby = 0;
        $longitude = '';
        $latitude = '';
        $area_id = $this->optionalParam('area_id');
        $district_ids = $this->optionalParam('district_ids');
//        $nearby = $this->optionalParam('nearby');
//        $longitude = $this->optionalParam('longitude');
//        $latitude = $this->optionalParam('latitude');

        $sel_data = [
            'price_min' => $this->optionalParam('price_min'),
            'price_max' => $this->optionalParam('price_max'),
            'bedroom' => $this->optionalParam('bedroom'),
            'restroom' => $this->optionalParam('restroom'),
            'build_area' => $this->optionalParam('build_area'),
            'house_direct' => $this->optionalParam('house_direct'),
            'house_analysis' => $this->optionalParam('house_analysis'),
            'house_tags' => $this->optionalParam('house_tags'),
            'floor_tag' => $this->optionalParam('floor_tag'),
            'build_age' => $this->optionalParam('build_age'),
            'house_decorate' => $this->optionalParam('house_decorate'),
            'house_purpose' => $this->optionalParam('house_purpose'),
            'heating_type' => $this->optionalParam('heating_type'),
            'trade_type' => $this->optionalParam('trade_type'),
            'build_type' => $this->optionalParam('build_type'),
        ];
        $data = MapSearch::houseSearchList(1,$city_id,$area_id,$district_ids,$nearby,$longitude,$latitude,$sel_data);
        $this->insertResponse('list',$data);
        return $this->response('二手房地图搜索列表');
    }

    private function mapRentalList()
    {
        $city_id = $this->essentialParam('city_id');
        //模仿贝壳 贝壳的区域字段变动并没有重新选择数据 若客户要求可以使用下面注释接收变量
        $area_id = 0;
        $district_ids = '';
        $nearby = 0;
        $longitude = '';
        $latitude = '';
//        $area_id = $this->optionalParam('area_id');
        $district_ids = $this->optionalParam('district_ids');
//        $nearby = $this->optionalParam('nearby');
//        $longitude = $this->optionalParam('longitude');
//        $latitude = $this->optionalParam('latitude');
        $sel_data = [
            'rental_type' => $this->optionalParam('rental_type'),
            'bedroom' => $this->optionalParam('bedroom'),
            'house_tags' => $this->optionalParam('house_tags'),
            'house_direct' => $this->optionalParam('house_direct'),
            'build_area' => $this->optionalParam('build_area'),

            'price_min' => $this->optionalParam('price_min'),
            'price_max' => $this->optionalParam('price_max'),

            'floor_tag' => $this->optionalParam('floor_tag'),
            'facility' => $this->optionalParam('facility'),
            'light' => $this->optionalParam('light'),
            'check_in' => $this->optionalParam('check_in'),
        ];

        $data = MapSearch::rentalSearchList($city_id,$area_id,$district_ids,$nearby,$longitude,$latitude,$sel_data);
        $this->insertResponse('list',$data);
        return $this->response('租房地图搜索列表');
    }
}
